package uit.mmt02.dataAccess;

import javax.mail.Message;

import uit.mmt02.doituong.OneMail;
import uit.mmt02.xuly.ConvertMessageToList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class db_email 
{
	
//	final static String DB_EMAIL_NAME = "db_emailqw";
	final static String table_name = "table_store_email";
	final static int VERSION = 1;
	
	final static String ID = "_id";
	final static String FROM = "_from";
	final static String TO = "_to";
	final static String SUB = "_sub";
	final static String BODY = "_body";
	final static String TIME = "_time";
	final static String ATTACH = "_attach";
	final static String FAVORITE = "_favorite";
	final static String ENCRYPT = "_encrypt";
	final static String VOICE = "_voice";
	//
	Context context;
	
	String db_name;
	String sql;
	String tag = "db_email";
	SQLiteDatabase mydb;
	db_helper my_db_helper;
	public db_email(Context context, String db_name) {
		super();
		this.context = context;
		this.db_name = db_name;
	}


	public class db_helper extends SQLiteOpenHelper
	{

		public db_helper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			
			// TODO Auto-generated method stub
			sql = "create table " + table_name + 
					"(_id integer PRIMARY KEY autoincrement, " +
					"_from text, " +
					"_to text, "+
					"_sub text, "+
					"_body text, "+
					"_time text, "+
					"_attach text, "+
					"_favorite numeric, "+
					"_encrypt numeric, " +
					"_voice numeric" + 
					");";
			db.execSQL(sql);
			Log.v(tag, "Khoi tao table : " + table_name);
					
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			sql = "drop table if exists "+table_name;
			db.execSQL(sql);
			Log.v(tag, "Ungrade " + table_name);
			
		}
		
	} //end class db_helper
	
	public db_email open()
	{
		my_db_helper = new db_helper(context, db_name, null, VERSION);
		mydb = my_db_helper.getWritableDatabase();
		Log.v(tag, "Open");
		return this;
	}
	
	public void close()
	{
		my_db_helper.close();
		Log.v(tag, "Close");
	}
	
	public long insert(OneMail om)
	{
		long row_insert = 0;
		try
		{
			ContentValues cv = new ContentValues();
			cv.put(FROM, om.getFrom());
			cv.put(TO, om.getTo());
			cv.put(SUB, om.getSub());
			cv.put(BODY, om.getBody());
			cv.put(TIME, om.getDateSend());
			cv.put(ATTACH, om.getChuoiAttach());
			cv.put(FAVORITE,om.isSao());
			cv.put(ENCRYPT, om.isFlagEncrypt());
			cv.put(VOICE, om.isFlagVoice());
			
			row_insert = mydb.insert(table_name, null, cv);
			Log.v(tag, "insert thanh cong " + om.getFrom());
		}catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "Loi insert " +e.toString());
		}
		return row_insert;
	}
	
	public Cursor query()
	{
		try
		{
			Log.v(tag, "get thanh cong " );
			return mydb.query(table_name, new String[]{ID,FROM,TO,SUB,BODY,TIME,ATTACH,FAVORITE,ENCRYPT,VOICE}, null, null, null, null, null);
			
		}catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "Loi get " +e.toString());
			return null;
		}
	}
	public boolean removeALl()
	{
		Log.i(tag, "remove CSDL my email...");
		return mydb.delete(table_name, null, null)>0;
	}
	public void save_msg_to_db(Message[] mess)
	{
		try
		{
//			db_email mydb = new db_email(context, table);
//			String []str = username.split("@");
//			table = "db_" + str[0] + "_gmail";
//			mydb = new db_email(context, table);
//			mydb.open();
//			open();
			ConvertMessageToList cv = new ConvertMessageToList();
			
			// luu 10 mail moi nhat vao db
			for(int i = mess.length -1 ; i > mess.length - 11 ; i --)
			{
				if(i > 0)
				{
					insert(cv.get1mail(mess, i));
					Log.v(tag, "insert mail: " + i );
				}
			}
//			close();
		}
		catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "Loi save mail to db" + e.toString());
		}
	}
	
	

}
